import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../app/routes.dart';

class SplashPage extends StatefulWidget {
  const SplashPage({super.key});

  @override
  State<SplashPage> createState() => _SplashPageState();
}

class _SplashPageState extends State<SplashPage> with SingleTickerProviderStateMixin {
  late AnimationController _controller;
  late Animation<double> _logoAnimation;
  late Animation<double> _starScaleAnimation;
  late Animation<double> _starOpacityAnimation;
  late Animation<double> _bgFadeAnimation;

  // 添加错误状态跟踪
  bool _hasError = false;
  String _errorMessage = '';

  @override
  void initState() {
    super.initState();

    _controller = AnimationController(
      duration: const Duration(milliseconds: 3000),
      vsync: this,
    );

    // 调整动画时序 - 让背景立即显示
    _bgFadeAnimation = Tween<double>(begin: 0.0, end: 1.0).animate(
      CurvedAnimation(
        parent: _controller,
        curve: const Interval(0.0, 0.3, curve: Curves.easeIn), // 延长到0.3秒
      ),
    );

    // Logo动画稍微延后开始
    _logoAnimation = Tween<double>(begin: 0.3, end: 1.0).animate(
      CurvedAnimation(
        parent: _controller,
        curve: const Interval(0.2, 0.7, curve: Curves.elasticOut), // 从0.2秒开始
      ),
    );

    _starScaleAnimation = Tween<double>(begin: 0.0, end: 1.2).animate(
      CurvedAnimation(
        parent: _controller,
        curve: const Interval(0.5, 0.9, curve: Curves.elasticOut),
      ),
    );

    _starOpacityAnimation = TweenSequence<double>([
      TweenSequenceItem(tween: Tween(begin: 0.0, end: 1.0), weight: 50),
      TweenSequenceItem(tween: Tween(begin: 1.0, end: 0.7), weight: 25),
      TweenSequenceItem(tween: Tween(begin: 0.7, end: 1.0), weight: 25),
    ]).animate(CurvedAnimation(parent: _controller, curve: const Interval(0.5, 1.0)));

    // 启动动画
    _controller.forward();

    // 3秒后跳转
    Future.delayed(const Duration(seconds: 3), () {
      if (mounted) Get.offNamed(Routes.scan);
    });
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  // 图片加载错误处理
  Widget _buildImageWithErrorHandling(String path, double width, double height) {
    return Image.asset(
      path,
      width: width,
      height: height,
      fit: BoxFit.contain,
      errorBuilder: (context, error, stackTrace) {
        // 图片加载失败时显示错误信息
        if (mounted) {
          WidgetsBinding.instance.addPostFrameCallback((_) {
            setState(() {
              _hasError = true;
              _errorMessage = '图片加载失败: $path\n错误: $error';
            });
          });
        }
        return Container(
          width: width,
          height: height,
          color: Colors.red,
          child: const Icon(Icons.error, color: Colors.white),
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.black,
      body: Stack(
        fit: StackFit.expand,
        children: [
          // 背景图 + 渐显
          AnimatedBuilder(
            animation: _controller,
            builder: (context, child) {
              return Opacity(
                opacity: _bgFadeAnimation.value,
                child: _buildImageWithErrorHandling(
                  'assets/icon/backgroundImg.png',
                  double.infinity,
                  double.infinity,
                ),
              );
            },
          ),

          // Logo 动画
          Center(
            child: AnimatedBuilder(
              animation: _controller,
              builder: (context, child) {
                return Stack(
                  alignment: Alignment.center,
                  children: [
                    Transform.scale(
                      scale: _logoAnimation.value,
                      child: _buildImageWithErrorHandling(
                        'assets/icon/logo-logo.png', // 确认文件名是否正确
                        200,
                        200,
                      ),
                    ),
                    Positioned(
                      top: 10,
                      right: 25,
                      child: Opacity(
                        opacity: _starOpacityAnimation.value,
                        child: Transform.scale(
                          scale: _starScaleAnimation.value,
                          child: _buildImageWithErrorHandling(
                            'assets/icon/star.png',
                            50,
                            50,
                          ),
                        ),
                      ),
                    ),
                  ],
                );
              },
            ),
          ),

          // 调试信息显示
          if (_hasError)
            Positioned(
              top: 100,
              left: 20,
              right: 20,
              child: Container(
                padding: const EdgeInsets.all(16),
                color: Colors.red.withOpacity(0.8),
                child: Text(
                  _errorMessage,
                  style: const TextStyle(color: Colors.white, fontSize: 12),
                  textAlign: TextAlign.center,
                ),
              ),
            ),

          // 动画进度调试信息
          Positioned(
            bottom: 20,
            left: 20,
            child: AnimatedBuilder(
              animation: _controller,
              builder: (context, child) {
                return Text(
                  '进度: ${_controller.value.toStringAsFixed(2)}\n'
                      'Logo缩放: ${_logoAnimation.value.toStringAsFixed(2)}\n'
                      '背景透明度: ${_bgFadeAnimation.value.toStringAsFixed(2)}',
                  style: const TextStyle(color: Colors.white, fontSize: 12),
                );
              },
            ),
          ),
        ],
      ),
    );
  }
}